package ynu.edu.elmspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ynu.edu.elmspringboot.po.Business;
import ynu.edu.elmspringboot.po.Food;
import ynu.edu.elmspringboot.po.OrderDetailet;
import ynu.edu.elmspringboot.po.Orders;
import ynu.edu.elmspringboot.service.BusinessService;
import ynu.edu.elmspringboot.service.FoodService;
import ynu.edu.elmspringboot.service.OrderDetailetService;
import ynu.edu.elmspringboot.service.OrdersService;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/OrdersController")
@Slf4j
@Api(tags = "订单管理")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private OrderDetailetService orderDetailetService;
    @Autowired
    private FoodService foodService;

    @ApiOperation( "创建订单")
    @PostMapping("/createOrders")
    public int createOrders(Orders orders) {
        orders.setOrderDate(String.valueOf(LocalDate.now()));

        // 保存订单
        boolean isSaved = ordersService.save(orders);

        if (isSaved && orderDetailetService.saveByCart(orders.getOrderId(), orders.getUserId())) {
            // 获取保存后的订单ID
            int orderId = orders.getOrderId();
            System.out.println("订单ID: " + orderId); // 打印订单 ID
            return orderId; // 返回订单 ID
        } else {
            return 0; // 返回 0 表示失败
        }
    }

    @ApiOperation( "根据订单ID查询订单信息")
    @PostMapping("/getOrdersById")
    public Orders getOrdersById(Integer orderId) {
        log.info("订单ID：{}", orderId);
        // 1. 获取订单基本信息
        Orders orders = ordersService.getById(orderId);
        if(orders != null) {
            // 2. 获取商家信息
            Business business = businessService.getById(orders.getBusinessId());
            orders.setBusiness(business);

            // 3. 获取订单明细
            List<OrderDetailet> list = orderDetailetService.list(new QueryWrapper<OrderDetailet>().eq("order_id", orderId));
            // 4. 为每个订单明细项获取食品信息
            for(OrderDetailet od : list) {
                Food food = foodService.getById(od.getFoodId());
                od.setFood(food);
            }
            orders.setList(list);
        }
        return orders;
    }

    @ApiOperation( "根据用户ID查询订单信息")
    @PostMapping("/listOrdersByUserId")
    public List<Orders> listOrdersByUserId(String userId) {
        return ordersService.list(new QueryWrapper<Orders>().eq("user_id", userId));
    }

    @ApiOperation( "根据订单ID查询订单明细信息")
    @PostMapping("/getOrderDetailetByOrderId")
    public List<OrderDetailet> getOrderDetailetByOrderId(Integer orderId) {
        // 根据订单ID查询订单明细
        QueryWrapper<OrderDetailet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        return orderDetailetService.list(queryWrapper);
    }

    @ApiOperation( "更新订单状态")
    @PostMapping("/updateOrderState")
    public boolean updateOrderState(Integer orderId, Integer orderState) {
//        Orders orders = ordersService.getById(orderId);
//        if (orders != null) {
//            orders.setOrderState(orderState);
//            return ordersService.updateById(orders);
//        }
//        return false;

        return ordersService.updateAndDeleteCart(orderId,orderState);
    }

}

